package com.example.kingdee.KingdeeCreate.impl;

import com.example.kingdee.KingdeeCreate.CreateBill;
import com.example.kingdee.KingdeeCreate.dto.CreateInDto;
import com.example.kingdee.exception.ProcedureException;
import com.example.kingdee.kingdeeBridge.common.RequestResult;
import com.example.kingdee.kingdeeBridge.dto.PushDto;
import com.example.kingdee.kingdeeBridge.kingdeeAbs.FolderKingdee;
import com.example.kingdee.kingdeeBridge.kingdeeImpl.impl.FeedMtrlImpl;
import com.example.kingdee.kingdeeBridge.kingdeeImpl.impl.ProInStockImpl;
import com.example.kingdee.sqlTool.SqlTool;
import com.example.kingdee.sqlTool.common.DataObject;
import com.example.kingdee.sqlTool.sqltoolEnum.FormIdEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 创建生产订单->生产领料->生产入库
 */
@Service
public class CreateProBillChainImpl implements CreateBill {

    @Autowired
    private SqlTool sqlTool;
    @Autowired
    private CreateProBillImpl createProBill;
    @Autowired
    private FeedMtrlImpl feedMtrl;

    @Override
    public void preposition(CreateInDto createInDto) {

    }

    @Override
    public RequestResult createSrcBill(CreateInDto createInDto) {
        //创建生产订单
//        CreateProBillImpl createProBill = new CreateProBillImpl();
        RequestResult moBill = createProBill.createSrcBill(createInDto);
        try {
            if (moBill.getCode().equals("200")){
                //审核订单
                createProBill.auditTargetBill(moBill.getNumber());
            }else {
                //删除对应单据
                createProBill.deleteTargetBill(moBill.getId());
                throw new ProcedureException(moBill.getMsg() + "，单据类型为：" + createInDto.getBillType() + ", 数据为：" + createInDto);
            }
        }catch (Exception e){
            //删除对应单据
            createProBill.deleteTargetBill(moBill.getId());
            throw new ProcedureException(moBill.getMsg() + "，单据类型为：" + createInDto.getBillType() + ", 数据为：" + createInDto);
        }
        //生产订单下推生产领料
        RequestResult mtrlRequestResult = moToMtrl(moBill);
        //生产订单下推生产入库
        RequestResult inStockRequestResult = moToMoInStock(moBill, mtrlRequestResult);
        return moBill;
    }

    @Override
    public void auditTargetBill(String billNo) {

    }

    @Override
    public void postPosition(RequestResult srcBill, CreateInDto createInDto) {

    }

    @Override
    public void deleteTargetBill(String fid) {

    }

    //生产订单下推生产领料
    public RequestResult moToMtrl(RequestResult moBill){
        //生产订单下推生产领料
        List<String> numbers = new ArrayList<>();
        numbers.add(moBill.getNumber());
        PushDto pushDto = new PushDto();
        pushDto.setSrcFormId(FormIdEnum.proMo);
        pushDto.setTargetFormId(FormIdEnum.prdPickMtrl);
        pushDto.setEnableDefaultRule(true);
        pushDto.setNumbers(numbers);
        FolderKingdee folderKingdee = new FolderKingdee(feedMtrl);
        RequestResult requestResult = new RequestResult();
        try {
            requestResult = folderKingdee.pushDown(pushDto);
            try {
                //审核领料单
                folderKingdee.auditTargetBill(requestResult.getNumber());
            }catch (ProcedureException e){
                //删除领料单
                folderKingdee.deleteTargetBill(String.valueOf(requestResult.getId()));
                //反审核生产订单
                DataObject unAuditDataObject = new DataObject(FormIdEnum.proMo);
                unAuditDataObject.addNumbers(moBill.getNumber());
                sqlTool.unAuditBatchData(unAuditDataObject);
                //删除生产订单
                createProBill.deleteTargetBill(moBill.getId());
            }
        }catch (Exception e){
            //删除领料单
            folderKingdee.deleteTargetBill(String.valueOf(requestResult.getId()));
            //反审核生产订单
            DataObject unAuditDataObject = new DataObject(FormIdEnum.proMo);
            unAuditDataObject.addNumbers(moBill.getNumber());
            sqlTool.unAuditBatchData(unAuditDataObject);
            //删除生产订单
            createProBill.deleteTargetBill(moBill.getId());
        }
        return requestResult;
    }

    //生产订单下推生产入库
    public RequestResult moToMoInStock(RequestResult moBill, RequestResult mtrlBill){
        //生产订单下推生产领料
        List<String> numbers = new ArrayList<>();
        numbers.add(moBill.getNumber());
        PushDto pushDto = new PushDto();
        pushDto.setSrcFormId(FormIdEnum.proMo);
        pushDto.setTargetFormId(FormIdEnum.proInStock);
        pushDto.setEnableDefaultRule(true);
        pushDto.setNumbers(numbers);
        FolderKingdee folderKingdee = new FolderKingdee(new ProInStockImpl());
        RequestResult requestResult = new RequestResult();
        try {
            requestResult = folderKingdee.pushDown(pushDto);
            try {
                //审核入库单
                folderKingdee.auditTargetBill(requestResult.getNumber());
            }catch (ProcedureException e){
                //删除入库单
                folderKingdee.deleteTargetBill(String.valueOf(requestResult.getId()));
                //反审核生产领料单
                DataObject mtrlNnAuditDataObject = new DataObject(FormIdEnum.prdPickMtrl);
                mtrlNnAuditDataObject.addNumbers(mtrlBill.getNumber());
                sqlTool.unAuditBatchData(mtrlNnAuditDataObject);
                //删除生产领料单
                FeedMtrlImpl feedMtrl = new FeedMtrlImpl();
                feedMtrl.deleteTargetBill(mtrlBill.getId());
                //反审核生产订单
                DataObject unAuditDataObject = new DataObject(FormIdEnum.proMo);
                unAuditDataObject.addNumbers(moBill.getNumber());
                sqlTool.unAuditBatchData(unAuditDataObject);
                //删除生产订单
                createProBill.deleteTargetBill(moBill.getId());
            }
        }catch (Exception e){
            //删除入库单
            folderKingdee.deleteTargetBill(String.valueOf(requestResult.getId()));
            //反审核生产领料单
            DataObject mtrlNnAuditDataObject = new DataObject(FormIdEnum.prdPickMtrl);
            mtrlNnAuditDataObject.addNumbers(mtrlBill.getNumber());
            sqlTool.unAuditBatchData(mtrlNnAuditDataObject);
            //删除生产领料单
            FeedMtrlImpl feedMtrl = new FeedMtrlImpl();
            feedMtrl.deleteTargetBill(mtrlBill.getId());
            //反审核生产订单
            DataObject unAuditDataObject = new DataObject(FormIdEnum.proMo);
            unAuditDataObject.addNumbers(moBill.getNumber());
            sqlTool.unAuditBatchData(unAuditDataObject);
            //删除生产订单
            createProBill.deleteTargetBill(moBill.getId());
        }
        return requestResult;
    }
}
